package com.ebai.product.management.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ebai.product.management.base.controller.WebController;
import com.ebai.product.management.base.security.BasicSecurityManager;
import com.ebai.product.management.base.utils.Common;
import com.ebai.product.management.framework.utils.security.ShiroUtils;
import com.ebai.product.management.model.entity.User;
import com.ebai.product.management.service.system.AuthService;
import com.ebai.product.management.service.system.UserService;

import net.sf.json.JSONObject;

@Controller
public class PortalController extends WebController {

    private Logger logger = Logger.getLogger(this.getClass());

    @Value("${system.title}")
    private String systemTitle;

    @Autowired
    private BasicSecurityManager basicSecurityManager;

    @Autowired
    private UserService userService;
    
    @Autowired
    private AuthService authService;

    @RequestMapping("/portal")
    public String portalPage(ModelMap modelMap) {
        User user = ShiroUtils.getUser();
        modelMap.addAttribute("page_title", systemTitle);
        modelMap.addAttribute("menus", authService.selectAuthsByUser(user));
        return "portal";
    }

    /**
     * Session过期检查
     * @param session
     * @param response
     */
    @RequestMapping("/chkSession")
    public void chkSession(HttpSession session, HttpServletResponse response) {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        User user = this.basicSecurityManager.getCurrentUser(session);
        if (null != user) {
            jsonMap.put("success", true);
            jsonMap.put("data", user);
        }
        else {
            jsonMap.put("success", false);
            jsonMap.put("data", "");
        }
        JSONObject json = Common.getJsonResult(jsonMap);
        this.sendDirectToClient(response, CONTENTTYPE_JSON, json.toString());
    }

    @RequestMapping("/home/console.html")
    public String consolePage(ModelMap modelMap) {
        return "home/console";
    }

    @RequestMapping("/home/message.html")
    public String messagePage(ModelMap modelMap) {
        return "home/message";
    }

    @RequestMapping("/home/password.html")
    public String passwordPage(ModelMap modelMap) {
        return "home/password";
    }

    /**
     * 修改当前用户密码
     * @param session
     * @param response
     * @param oldPasswd
     * @param newPasswd
     */
    @RequestMapping("/home/setPasswd")
    public void setPasswd(HttpSession session, HttpServletResponse response, String oldPasswd, String newPasswd) {
        Map<String, Object> result = this.basicSecurityManager.modifyPasswd(session, oldPasswd, newPasswd);
        JSONObject json = Common.getJsonResult(result);
        this.sendDirectToClient(response, CONTENTTYPE_JSON, json.toString());
    }

    @RequestMapping("/home/theme.html")
    public String themePage(ModelMap modelMap) {
        return "home/theme";
    }

    /**
     * 设置主题
     */
    @RequestMapping("/home/setTheme.html")
    public String setTheme(String themeName, HttpServletRequest request) {
        if (null == themeName) {
            request.getSession().removeAttribute("theme");
        } else {
            request.getSession().setAttribute("theme", themeName);
        }
        return "redirect:/portal.html";
    }

    @RequestMapping("/home/personal.html")
    public String personalPage(HttpSession session, ModelMap modelMap) {
//        User user = this.basicSecurityManager.getCurrentUser(session);
        modelMap.addAttribute("user", this.userService.queryByLoginName(ShiroUtils.getLoginName()));
        return "home/personal";
    }

    /**
     * 修改当前用户信息
     * @param session
     * @param response
     * @param entity
     */
    @RequestMapping("/home/setPersonal")
    public void setPersonal(HttpSession session, HttpServletResponse response, User entity) {
        Map<String, Object> result = this.userService.saveData(entity);
        JSONObject json = Common.getJsonResult(result);
        this.sendDirectToClient(response, CONTENTTYPE_JSON, json.toString());
    }
}
